#!/usr/bin/env python3
"""
调试AI API响应格式
"""

import asyncio
import aiohttp
import json

async def debug_ai_api_response():
    """直接调试AI API的响应"""
    print("=== 调试AI API响应格式 ===")
    
    try:
        async with aiohttp.ClientSession() as session:
            # 直接调用AI API
            request_data = {
                "model": "S1-Base-32B",
                "prompt": "你好，请简单介绍一下硅材料",
                "stream": True,
                "max_tokens": 100,
                "temperature": 0.7,
                "stop": ["<|im_end|>"]
            }
            
            async with session.post(
                'http://office20.inoteexpress.com:8003/v1/completions',
                json=request_data,
                headers={"Content-Type": "application/json"}
            ) as response:
                
                print(f"AI API响应状态: {response.status}")
                print(f"AI API响应头: {dict(response.headers)}")
                
                # 读取流式响应
                async for chunk in response.content:
                    if chunk.strip():
                        chunk_text = chunk.decode('utf-8')
                        print(f"AI API原始响应块: {repr(chunk_text)}")
                        
                        # 尝试解析JSON
                        try:
                            if chunk_text.startswith('data: '):
                                json_str = chunk_text[6:].strip()
                                data = json.loads(json_str)
                                print(f"解析后的JSON: {json.dumps(data, ensure_ascii=False, indent=2)}")
                            else:
                                data = json.loads(chunk_text)
                                print(f"直接解析的JSON: {json.dumps(data, ensure_ascii=False, indent=2)}")
                        except json.JSONDecodeError as e:
                            print(f"JSON解析错误: {e}")
                            print(f"原始文本: {chunk_text}")
                
    except Exception as e:
        print(f"调试失败: {e}")

async def main():
    await debug_ai_api_response()

if __name__ == "__main__":
    asyncio.run(main())